package com.chixing.service.impl;

import com.chixing.mapper.FlowMapper;
import com.chixing.pojo.Flow;
import com.chixing.pojo.FlowExample;
import com.chixing.service.FlowService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author Wesley
 * @version 1.0
 * @date 2021/10/5 15:31
 */
@Service
public class FlowServiceImpl implements FlowService {
    @Autowired
    private FlowMapper flowMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public ServerResponse getById(Integer flowId) {
        String redisFlowKey = "flowId_" + flowId;
        Flow flow = null;
        if(!redisTemplate.hasKey(redisFlowKey)){
            flow = flowMapper.selectByPrimaryKey(flowId);
        }else {
            flow = (Flow) redisTemplate.opsForValue().get(redisFlowKey);
        }
        return ServerResponse.getSuccess(flow);
    }

    @Override
    public ServerResponse save(Long flowNumber,Integer custId, Integer myOrderId, Float orderPrice) {
        Flow flow = new Flow();

//        //利用UUID生成订单号
//        String[] chars = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
//        StringBuffer shortBuffer = new StringBuffer();
//        String uuid = UUID.randomUUID().toString().replace("-","");
//        for (int j=0;j<16;j++) {
//            String str=uuid.substring(j*2,j*2+2);
//            int par=Integer.parseInt(str,24);
//            shortBuffer.append(chars[par % 10]);
//        }
//        Long flowNum = Long.valueOf(shortBuffer.toString());

        //利用UUID生成回执编号
        String[] chars1 = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9","A","B","C","D","E","F","G",
                "H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        StringBuffer shortBuffer1 = new StringBuffer();
        String uuid1 = UUID.randomUUID().toString().replace("-","");
        for (int j=0;j<6;j++) {
            String str=uuid1.substring(j*4,j*4+4);
            int par=Integer.parseInt(str,36);
            shortBuffer1.append(chars1[par % 36]);
        }
        String receiptNumber  = shortBuffer1.toString();
        flow.setFlowNumber(flowNumber);
        flow.setCustId(custId);
        flow.setMyOrderId(myOrderId);
        flow.setPayTime( new Date());
        flow.setPayAmount(orderPrice);
        flow.setReceiptNumber(receiptNumber);
        flow.setStatus(0);
//        int insert = flowMapper.insert(flow);
        String redisFlowKey = "flowId_" + flow.getFlowNumber();
        String redisFlowKey2 = "flowId2_" + flow.getFlowNumber();
        redisTemplate.opsForValue().set(redisFlowKey2,"",3, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set(redisFlowKey,flow);

        return ServerResponse.saveSuccess(flow);
    }

    @Override
    public ServerResponse getFlowByFlowNum(Long flowNumber) {
        String redisFlowKey = "flowId_" + flowNumber;
        Flow flow = null;
        if(redisTemplate.hasKey((redisFlowKey)))
            flow = (Flow)redisTemplate.opsForValue().get(redisFlowKey);
        else {
            FlowExample flowExample = new FlowExample();
            flowExample.createCriteria().andFlowNumberEqualTo(flowNumber);
            List<Flow> flowList = flowMapper.selectByExample(flowExample);
            if(flowList.size()>0)
                flow = flowList.get(0);
        }
        if(flow != null)
            return ServerResponse.getSuccess(flow);
        return ServerResponse.getFail(null);
    }


}
